package com.psy.counseling.utils;

import com.psy.counseling.entity.Question;
import com.psy.counseling.service.QuestionService;
import lombok.Data;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * <p>
 *  导入工具类
 * </p>
 *
 * @author 陈灿
 * @since 2022-06-07
 */
@Data
public class ImportQuestionUtil {

    @Resource
    private QuestionService questionService;
    private ArrayList<Question> importSuccess;
    private ArrayList<Integer> importError;

    public ImportQuestionUtil(FileInputStream inputStream) throws IOException {
        importError = new ArrayList<>();
        importSuccess = new ArrayList<Question>();
        ImportData(inputStream);
    }

    public void ImportData(FileInputStream inputStream) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            DecimalFormat df = new DecimalFormat("#");
            XSSFCell idCell = row.getCell(0);
            XSSFCell questionInfoCell = row.getCell(1);
            XSSFCell aOptionCell = row.getCell(2);
            XSSFCell bOptionCell = row.getCell(3);
            XSSFCell cOptionCell = row.getCell(4);
            XSSFCell dOptionCell = row.getCell(5);
            XSSFCell aWeightCell = row.getCell(6);
            XSSFCell bWeightCell = row.getCell(7);
            XSSFCell cWeightCell = row.getCell(8);
            XSSFCell dWeightCell = row.getCell(9);
            if (idCell==null || questionInfoCell == null || aOptionCell==null || bOptionCell == null
                    || cOptionCell == null || dOptionCell == null || aWeightCell == null || bWeightCell == null || cWeightCell == null || dWeightCell == null) {
                importError.add(i+1);
                continue;
            }
            Question question = new Question();
            String id = df.format(Double.parseDouble(idCell.toString()));
            question.setId(Long.valueOf(id));


            question.setQuestionInfo(String.valueOf(questionInfoCell));


            question.setAOption(String.valueOf(aOptionCell));


            question.setBOption(String.valueOf(bOptionCell));

            question.setCOption(String.valueOf(cOptionCell));


            question.setDOption(String.valueOf(dOptionCell));


            question.setAWeight(Integer.valueOf(df.format(Double.parseDouble(aWeightCell.toString()))));

            question.setBWeight(Integer.valueOf(df.format(Double.parseDouble(bWeightCell.toString()))));

            question.setCWeight(Integer.valueOf(df.format(Double.parseDouble(cWeightCell.toString()))));


            question.setDWeight(Integer.valueOf(df.format(Double.parseDouble(dWeightCell.toString()))));

            importSuccess.add(question);
        }
    }

}